﻿using System;
using System.IO;
using SportTraining.Core.Domain;

namespace SportTraining.MVVM.Model.Impl
{
    public class PaymentTransaction
    {
        #region private var

        private const string transactionFile = "transaction.xml";

        private readonly string path2TransactionFile = string.Format("{0}\\{1}", AppDomain.CurrentDomain.BaseDirectory,
                                                                     transactionFile);

        private TransactionInfo info;

        #endregion

        #region constructor
        public PaymentTransaction()
        {
            state = TransactionState.Empty;
        }
        #endregion

        #region public properties

        private TransactionState state = TransactionState.Empty;


        public TransactionState State
        {
            get { return state; }
            set
            {
                state = value;
                SaveTransaction();
            }
        }

        public IPayment payment;

        public IPayment Payment
        {
            get { return payment; }
            set
            {
                payment = value;
                info = new TransactionInfo(payment, State);
            }
        }

        public int PriceId
        {
            get { return info.PriceId; }
        }

        #endregion

        #region method

        public void Clear()
        {
            if (File.Exists(path2TransactionFile))
            {
                File.Delete(path2TransactionFile);
            }
        }


        private void SaveTransaction()
        {
            Console.WriteLine(Payment.ToString());

            info.UpdateInfo(Payment, State);

            Helpers.XmlUtility.SerializeObjectToDisk(path2TransactionFile, info);
        }

        #endregion
    }
}